package com.example.udpLearn.udp3;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
 * 客户端不断发送数据，服务器端不断接收数据：服务端
 */
public class Server {
    public static void main(String[] args) throws IOException {

        System.out.println("服务端启动...");

        /**
         * 定义服务端对象
         */
        DatagramSocket socket = new DatagramSocket(8888);

        /**
         * 接收数据
         */
        while (true) {
            byte[] bytes = new byte[1024 * 64];
            DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
            socket.receive(packet);
            System.out.println("服务端接收到数据：" + new String(packet.getData(), 0, packet.getLength()));
            System.out.println("服务端接收到的数据来自IP：" + packet.getAddress().getHostAddress());
            System.out.println("服务端接收到的数据来自Port" + packet.getPort());
            System.out.println("===========================================");
        }

    }
}
